package com.cicc.springboot.utile;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.cicc.springboot.entity.OrderItemEntity;
import com.cicc.springboot.entity.PurchaseOrderEntity;

import java.util.ArrayList;
import java.util.Map;

/**
 * 订单明细监听器
 *
 * @ClassName OrderExcelListener
 * @Description TODO
 * @Author 转轮王
 * @Date 2024/10/11 16:41
 * @Version 1.0
 */

public class OrderDetailListener extends AnalysisEventListener<OrderItemEntity> {
    // 存储订单信息的map，用于将订单明细添加到对应的订单中
    private Map<String, PurchaseOrderEntity> orderMap;

    // 通过构造方法传入订单信息的Map
    public OrderDetailListener(Map<String, PurchaseOrderEntity> orderMap) {
        this.orderMap = orderMap;
    }

    // 每读取一行订单明细时调用
    @Override
    public void invoke(OrderItemEntity orderDetail, AnalysisContext context) {
        // 根据订单编号找到对应的订单信息
        PurchaseOrderEntity orderInfo = orderMap.get(orderDetail.getOrderCode());
        if (orderInfo != null) {
            // 如果该订单还没有订单明细列表，则初始化
            if (orderInfo.getOrderDetails() == null) {
                orderInfo.setOrderDetails(new ArrayList<>());
            }
            // 将订单明细添加到订单的明细列表中
            orderInfo.getOrderDetails().add(orderDetail);
//            System.out.println("订单编号：" + orderDetail.getOrderCode() + " 的明细：" + orderDetail);
        } else {
            System.out.println("未找到对应的订单编号：" + orderDetail.getOrderCode());
        }
    }


    // 解析完成后执行的方法
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
    }
}
